//
//  EVCPopWindowView.m
//  EverVCiOSClient
//
//  Created by 张可可 on 15/11/11.
//  Copyright (c) 2015年 EverVC. All rights reserved.
//

#import "EVCPopWindowView.h"
//#import "UIImage+BlurredFrame.h"
#import "EVCPopWindowBtn.h"
#import "Masonry.h"
#import "NSString+QT.h"

#define PopActionItemSection_Height 50.0
#define PopActionItem_baseTag 31498

@interface EVCPopWindowView ()

@property (nonatomic, strong) PopWindowDidClickButtonAtIndex callBackBlock;
@property (nonatomic) CGFloat customViewHeight;
@property (nonatomic, strong) NSArray *titleArr;

@property (nonatomic, strong) NSString *descTitle;

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *lastTopView;

@end

@implementation EVCPopWindowView

- (instancetype)initWithTitle:(NSString *)title descTitle:(NSString *)descTitle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.titleArr = otherButtonTitles;
        self.backgroundColor = [UIColor clearColor];
        
        //  点击空白处，收回事件
        UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tapG];
        
        //  透明背景图
        self.bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.bgView.backgroundColor = [UIColor blackColor];
        self.bgView.alpha = 0.3f;
        [self addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.edges.equalTo(self);
            
        }];
        
        //  容器视图
        self.contentView = [[UIView alloc] initWithFrame:CGRectZero];
        [self addSubview:self.contentView];
        self.contentView.backgroundColor = UIColor.k_hexColor(0xf0f0f0);
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
        }];
        
        UIView *lastView = nil;
        //  取消按钮
        EVCPopWindowBtn *cancelBtn = [EVCPopWindowBtn buttonWithType:UIButtonTypeCustom title:cancelButtonTitle btnImage:nil titleColor:UIColor.k_t1Color bgColor:UIColor.k_hexColor(0xf5f5f5)];
        cancelBtn.tag = PopActionItem_baseTag + 0;
        [self.contentView addSubview:cancelBtn];
        [cancelBtn addTarget:self action:@selector(didClickPopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.height.equalTo(@(PopActionItemSection_Height));
            
        }];
        lastView = cancelBtn;
        
        //  取消 间隔线
        UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectZero];
        lineView1.backgroundColor = UIColor.k_hexColor(0xdfdfdf);
        
        [self.contentView addSubview:lineView1];
        
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.height.equalTo(@0.5);
            make.bottom.equalTo(cancelBtn.mas_top).mas_offset(-0.5f);
        }];
        
        
        lastView = lineView1;
        
        if (otherButtonTitles.count > 0) {
            
            UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectZero];
            lineView2.backgroundColor = UIColor.k_hexColor(0xdfdfdf);
            [self.contentView addSubview:lineView2];
            
            [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView);
                make.right.equalTo(self.contentView);
                make.height.equalTo(@0.5f);
                make.bottom.equalTo(lineView1.mas_top).with.offset(-6);
            }];
            
            lastView = lineView2;
            
        }
        
        
        //  Action 按钮
        for (int i = 0; i < otherButtonTitles.count; i++) {
            id obj = otherButtonTitles[i];
            NSString *title = nil;
            NSString *img = nil;
            if ([obj isKindOfClass:[NSDictionary class]] == YES) {
                title = obj[@"title"];
                img = obj[@"imageName"];
            } else {
                title = obj;
            }
            EVCPopWindowBtn *btn = [EVCPopWindowBtn buttonWithType:UIButtonTypeCustom title:title btnImage:img titleColor:UIColor.k_t1Color bgColor:UIColor.k_hexColor(0xf5f5f5)];
            [btn setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
            btn.tag = PopActionItem_baseTag + i + 1;
            [btn addTarget:self action:@selector(didClickPopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
            
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(self.contentView);
                make.right.equalTo(self.contentView);
                make.height.equalTo(@(PopActionItemSection_Height));
                make.bottom.equalTo(lastView.mas_top);
                
            }];
            if (img != nil && i == 1) {
                
                btn.contentEdgeInsets = UIEdgeInsetsMake(0, 18, 0, 0);
                
            }
            
            lastView = btn;
            //  如果当前不是最后一个btn， 添加一条间隔线
            if (i+1 < otherButtonTitles.count) {
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 0.5)];
                lineView.backgroundColor = UIColor.k_hexColor(0xeeeeee);
                [self.contentView addSubview:lineView];
                [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                   
                    make.left.equalTo(self.contentView);
                    make.right.equalTo(self.contentView);
                    make.height.equalTo(@0.5f);
                    make.bottom.equalTo(lastView.mas_top);
                    
                }];
                lastView = lineView;
            }
            
        }
        
        //  描述
        if (descTitle && descTitle.length > 0) {
            self.descTitle = descTitle;
            
            if (otherButtonTitles.count > 0) {
                
                //  描述和按钮之间的线
                UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 1)];
                lineView2.backgroundColor = UIColor.k_hexColor(0xdfdfdf);
                
                [self.contentView addSubview:lineView2];
                
                [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView);
                    make.right.equalTo(self.contentView);
                    make.height.equalTo(@0.5);
                    make.bottom.equalTo(lastView.mas_top).mas_offset(-0.5f);
                }];
                
                lastView = lineView2;
                
            }
            
            self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 0)];
            self.descLabel.numberOfLines = 0;
            self.descLabel.textAlignment = NSTextAlignmentCenter;
            self.descLabel.font = [UIFont systemFontOfSize:14];
            self.descLabel.textColor = UIColor.k_t2Color;
            [self setTitleColor:nil descLabelTextColor:UIColor.k_t2Color];
            
            
            [self.contentView addSubview:self.descLabel];
            [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).with.offset(15);
                make.right.equalTo(self.contentView).with.offset(-15);
                make.bottom.equalTo(lastView.mas_top).with.offset(-15);
            }];
            
            lastView = self.descLabel;
        }
        
        //  标题
        if (title && title.length > 0) {
            
            //  如果描述为空， 需要添加间隔线
            if (descTitle == nil || descTitle.length <= 0) {
                
                //  描述和按钮之间的线
                UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 1)];
                lineView2.backgroundColor = UIColor.k_hexColor(0xdfdfdf);
                
                [self.contentView addSubview:lineView2];
                
                [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView);
                    make.right.equalTo(self.contentView);
                    make.height.equalTo(@0.5);
                    make.bottom.equalTo(lastView.mas_top).mas_offset(-0.5f);
                }];
                
                lastView = lineView2;
                
            }
            
            
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 1)];
            self.titleLabel.font = [UIFont systemFontOfSize:15];
            self.titleLabel.textColor = UIColor.k_t1Color;
            self.titleLabel.text = title;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            [self.contentView addSubview:self.titleLabel];
            
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(self.contentView).with.offset(15);
                make.right.equalTo(self.contentView).with.offset(-15);
                if (self.descLabel != nil) {
                    make.bottom.equalTo(lastView.mas_top).with.offset(-10);
                } else {
                    make.bottom.equalTo(lastView.mas_top).with.offset(-15);
                }
            }];
            
            
            lastView = self.titleLabel;
        }
     
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([lastView isMemberOfClass:[UILabel class]]) {
                make.top.equalTo(lastView.mas_top).with.offset(-15);
            } else {
                make.top.equalTo(lastView.mas_top);
            }
        }];
        self.lastTopView = lastView;
        
    }
    return self;
}

- (void)didClickPopBtnAction:(EVCPopWindowBtn *)btn {

    if (self.callBackBlock) {
        self.callBackBlock(btn.tag - PopActionItem_baseTag);
    }
    [self remove];
}

- (void)addCustomDescView:(UIView *)descView viewHeight:(CGFloat)heght {
    
}

- (void)clickButtonAction:(PopWindowDidClickButtonAtIndex)btnAction {
    self.callBackBlock = btnAction;
}
- (void)tapAction:(UITapGestureRecognizer *)tapG {
    [self remove];
}


- (void)setButtonTitleColorAtIndex:(NSInteger)buttonIndex titleColor:(UIColor *)color {
    
    UIButton *btn = (UIButton *)[self.contentView viewWithTag:buttonIndex+PopActionItem_baseTag];
    [btn setTitleColor:color forState:UIControlStateNormal];
    
}

- (void)setTitleColor:(UIColor *)titleColor descLabelTextColor:(UIColor *)descTextColor {
    
    if (titleColor) {
        self.titleLabel.textColor = titleColor;
    }
    if (descTextColor && self.descTitle) {
        
        
        if ([self.descTitle isMoreThanOneLineWithSize:CGSizeMake(ScreenWidth - 30, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14] lineSpaceing:6] == NO) {
            self.descLabel.text = self.descTitle;
            return;
        }
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 6;
        paragraphStyle.alignment = NSTextAlignmentCenter;
        
        //            paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
        NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14],
                                      NSParagraphStyleAttributeName:paragraphStyle,
                                      NSForegroundColorAttributeName: descTextColor};
        self.descLabel.attributedText = [[NSAttributedString alloc] initWithString:self.descTitle attributes:attributes];
        
    }
    
}

- (void)show {
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    self.bgView.alpha = 0;
    self.contentView.width = ScreenSize.width;
    self.contentView.bottom = ScreenSize.height + 300;
//    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self);
//        make.right.equalTo(self);
//        make.bottom.equalTo(self).with.offset(-300);
//        make.top.equalTo(self.lastTopView);
//    }];
    [window addSubview:self];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.alpha = 0.3f;
        [self layoutIfNeeded];
    }];
}
- (void)remove {
    
    [UIView animateWithDuration:0.2 animations:^{
       
        self.bgView.alpha = 0;
        self.contentView.top = ScreenSize.height;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}

- (BOOL)isShowing
{
    BOOL ist = NO;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    for (UIView * view in window.subviews) {
        if ([view isKindOfClass:[self class]])
        {
            ist = YES;
        }
        else
        {
            ist = NO;
        }
    }
    return ist;
}

- (void)adjustShareBtnPosition {
    
    return;
    UIButton *btn = (UIButton *)[self.contentView viewWithTag:PopActionItem_baseTag+2];
    
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 18, 0, 0);
    
}

@end
